package org.javaboy.meeting.service;

import org.javaboy.meeting.mapper.EmployeeMapper;
import org.javaboy.meeting.model.Employee;
import org.javaboy.meeting.model.Pager;
import org.javaboy.meeting.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    public Employee doLogin(String username, String password) {
        Employee employee = employeeMapper.loadEmpByUsername(username);
        if (employee == null || !Md5.JM(employee.getPassword()).equals(password)) {
            return null;
        }
        return employee;
    }

    public Integer doReg(Employee employee) {
        Employee emp = employeeMapper.loadEmpByUsername(employee.getUsername());
        if (emp != null) {
            return -1;
        }
        employee.setRole(1);
        employee.setStatus(0);
        employee.setPassword(Md5.KL(employee.getPassword()));
        return employeeMapper.doReg(employee);
    }

    public List<Employee> getAllEmpsByStatus(Integer status) {
        return employeeMapper.getAllEmpsByStatus(status);
    }

    public Integer updatestatus(Integer employeeid, Integer status) {
        return employeeMapper.updatestatus(employeeid, status);
    }

    public List<Employee> getAllEmps(Employee employee, Integer page, Integer pageSize) {
        page = (page - 1) * pageSize;
        return employeeMapper.getAllEmps(employee,page,pageSize);
    }

    public Long getTotal(Employee employee) {
        return employeeMapper.getTotal(employee);
    }

    public List<Employee> getEmpByDepId(Integer depId) {
        return employeeMapper.getEmpByDepId(depId);
    }

    public List<Employee> getEmpByMeetingId(Integer meetingid) {
        return employeeMapper.getEmpByMeetingId(meetingid);
    }

    //分页新增
    public Pager<Employee> getEmpsbyPage(Integer start,Integer size,Integer status){
        Pager<Employee> pager = new Pager<Employee>();
        List<Employee> employees= employeeMapper.getEmpsbyPage(start,size,status);
        Integer count = employeeMapper.getCount(status);
        pager.setList(employees);
        pager.setRecordCount(count);//返回总记录数
        pager.setPageStart(start);//返回这一页开始项
        pager.setPageCount(count%10 == 0 ? count/10 : count/10+1);
        pager.setPageLastStart((pager.getPageCount()-1)*10);
        if((start-10)>0){
            pager.setPreviousStart(start-10);
        }else{
            pager.setPreviousStart(0);
        }
        if((start+10)<count){
            pager.setNextStart(start+10);
        }else{
            pager.setNextStart(start);
        }
        return pager;
    }

    public Pager<Employee> searchEmpsbyPage(Integer start,Integer size,String name,String username,Integer status){
        Pager<Employee> pager = new Pager<Employee>();
        List<Employee> employees= employeeMapper.searchEmpsbyPage(start,size,name,username,status);
        Integer count = employeeMapper.getSearchCount(name,username,status);
        pager.setList(employees);
        pager.setRecordCount(count);//返回总记录数
        pager.setPageStart(start);//返回这一页开始项
        pager.setPageCount(count%10 == 0 ? count/10 : count/10+1);
        pager.setPageLastStart((pager.getPageCount()-1)*10);
        if((start-10)>0){
            pager.setPreviousStart(start-10);
        }else{
            pager.setPreviousStart(0);
        }
        if((start+10)<count){
            pager.setNextStart(start+10);
        }else{
            pager.setNextStart(start);
        }
        return pager;
    }
}
